local skel = fk.CreateSkill {
  name = "emo__xiaolue",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,trick",
  prompt = function (self, player, selected_cards, selected)
    local n = player:getHandcardNum()
    if n > 0 and n <= 4 then
      return "#emo__xiaolue:::"..n..":"..(4-n)
    end
    return " "
  end,
  interaction = function(self, player)
    local all_choices = table.filter(Fk:getAllCardNames("bt"), function (name)
      local tname = Fk.all_card_types[name].trueName
      return Fk:translate(tname, "zh_CN"):len() == player:getHandcardNum()
    end)
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    local name = self.interaction.data
    if not name then return nil end
    local c = Fk:cloneCard(name)
    c.skillName = skel.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local n = player:getHandcardNum()
    use.extra_data = use.extra_data or {}
    use.extra_data.emo__xiaolue_num = 4 - n
    use.extra_data.emo__xiaolue_from = player.id
    room:setPlayerMark(player, "@emo__xiaolue", n)
    room:invalidateSkill(player, skel.name)
  end,
  enabled_at_play = function(self, player)
    return player:getHandcardNum() > 0 and player:getHandcardNum() <= 4
  end,
  enabled_at_response = function (self, player, response)
    if player:getHandcardNum() > 0 and player:getHandcardNum() <= 4 then
      if Fk.currentResponsePattern and not response then
        return table.find(Fk:getAllCardNames("bt"), function (name)
          local tname = Fk.all_card_types[name].trueName
          return Fk:translate(tname, "zh_CN"):len() == player:getHandcardNum()
          and Exppattern:Parse(Fk.currentResponsePattern):matchExp(tname)
        end)
      end
    end
  end,
})


skel:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local n = (data.extra_data or Util.DummyTable).emo__xiaolue_num
    return n and player:getHandcardNum() ~= n and player.id == (data.extra_data).emo__xiaolue_from
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = (data.extra_data or Util.DummyTable).emo__xiaolue_num
    local x = player:getHandcardNum() - n
    if x > 0 then
      room:askToDiscard(player, {
        max_num = x, min_num = x, skill_name = skel.name, include_equip = false, cancelable = false,
      })
    elseif x < 0 then
      player:drawCards(-x, skel.name)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@emo__xiaolue", 0)
  room:validateSkill(player, skel.name)
end)

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("@emo__xiaolue") ~= 0 and player:getHandcardNum() == player:getMark("@emo__xiaolue") then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand then return true end
        if move.from == player and table.find(move.moveInfo, function(info) return info.fromArea == Player.Hand end) then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@emo__xiaolue", 0)
    room:validateSkill(player, skel.name)
  end,
})

Fk:loadTranslationTable{
  ["emo__xiaolue"] = "枭略",
  [":emo__xiaolue"] = "当你需要使用基本牌或普通锦囊牌时，你可以视为使用一张牌名数为X的基本牌或普通锦囊牌，然后将手牌数摸或弃至4-X张，”枭略“失效直到你的手牌数增加或减少至X。（X为当前手牌数）",
  ["#emo__xiaolue"] = "枭略：视为使用一张%arg字牌，然后调整手牌至%arg2张",
  ["@emo__xiaolue"] = "枭略",
}


return skel
